package com.eco.fanliapp.admin.controller.web;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eco.fanliapp.admin.annotation.SysLogAnnotation;
import com.eco.fanliapp.admin.common.BaseController;
import com.eco.fanliapp.common.response.ServerResponse;
import com.eco.fanliapp.entity.SyTimePeriod;
import com.eco.fanliapp.service.ISyTimePeriodGoodsService;
import com.eco.fanliapp.service.ISyTimePeriodService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-09-09
 */
@RestController
@RequestMapping("/web/syTimePeriod")
public class SyTimePeriodWebController extends BaseController {

    @Autowired
    private ISyTimePeriodService iSyTimePeriodService;

    @Autowired
    private ISyTimePeriodGoodsService iSyTimePeriodGoodsService;

    @GetMapping(value = "homePageList")
    public ServerResponse homePageList(@RequestParam(defaultValue = "0") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize) {
        JSONObject jsonObject = new JSONObject();
        List<SyTimePeriod> syTimePeriodList = iSyTimePeriodService.syTimePeriodList(pageNum,pageSize,1,0);
        jsonObject.put("syTimePeriodList",syTimePeriodList);
        long sysTime = System.currentTimeMillis();
        long countDown = sysTime;
        for (SyTimePeriod syTimePeriod : syTimePeriodList) {
            Date periodStartTime = syTimePeriod.getPeriodStartTime();
            long startTime = periodStartTime.getTime();
            if(startTime > sysTime){
                countDown =  startTime - sysTime;
                break;
            }
        }
        jsonObject.put("countDown",countDown);
        return ServerResponse.createBySuccess("查询成功", jsonObject);
    }

    @GetMapping(value = "list")
    public ServerResponse list(@RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "20") Integer pageSize) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("enable", 1);
        queryWrapper.eq("deleted", 0);
        Page<SyTimePeriod> page = new Page<>(pageNum, pageSize);
        IPage iPage = iSyTimePeriodService.page(page, queryWrapper);
        return ServerResponse.createBySuccess("查询成功", iPage);
    }

    @PostMapping(value = "save")
    public ServerResponse save(@RequestBody SyTimePeriod syTimePeriod) {
        boolean b = iSyTimePeriodService.saveOrUpdate(syTimePeriod);
        if(b)return ServerResponse.createBySuccessMessage("操作成功");
        return ServerResponse.createByErrorMessage("操作失败");
    }

    @PostMapping(value = "saveNew")
    @SysLogAnnotation(value = "添加了秒杀时间段",userType = 2)
    public ServerResponse saveNew(@RequestBody SyTimePeriod syTimePeriod) {
        //校验参数不能为空
        String periodTime = syTimePeriod.getPeriodTime();
        if(periodTime ==null || "".equals(periodTime) ){
            return ServerResponse.createByErrorMessage("时间段不能空！");
        }

        //先校验名称，时间段是否重复
        QueryWrapper<SyTimePeriod> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("period_time",periodTime);
        queryWrapper.eq("enable",1);
        queryWrapper.eq("deleted",0);
        List<SyTimePeriod> list = iSyTimePeriodService.list(queryWrapper);
        Date date = new Date();
        //判断是修改还是更新
        if(CollectionUtils.isEmpty(list)){
            if(syTimePeriod.getTableId() == null){
                //新增
                SyTimePeriod sy = new SyTimePeriod();
                sy.setPeriodName("秒杀_" + periodTime);
                sy.setPeriodTime(periodTime);
                sy.setPeriodStartTime(date);
                sy.setPeriodEndTime(date);
                iSyTimePeriodService.save(sy);

            }else{
                //修改
                SyTimePeriod syt = iSyTimePeriodService.getById(syTimePeriod.getTableId());
                syt.setPeriodName("秒杀_" + periodTime);
                syt.setPeriodTime(periodTime);
                syt.setPeriodStartTime(date);
                syt.setPeriodEndTime(date);
                iSyTimePeriodService.updateById(syt);

            }
        }else{
            return ServerResponse.createByErrorMessage("时间段不允许重复！");
        }

        return ServerResponse.createBySuccess("操作成功");
    }

    @PostMapping(value = "delete")
    @SysLogAnnotation(value = "删除了秒杀时间段",userType = 2)
    public ServerResponse delete(@RequestBody SyTimePeriod syTimePeriod) {
        //校验参数不能为空
        Integer tableId = syTimePeriod.getTableId();
        if(tableId ==null){
            return ServerResponse.createByErrorMessage("时间段tableId不能空！");
        }
        //修改
        SyTimePeriod syt = iSyTimePeriodService.getById(tableId);
        if(syt == null){
            return ServerResponse.createByErrorMessage("该时间段id不存在！");
        }
        syt.setDeleted(1);
        iSyTimePeriodService.updateById(syt);
        return ServerResponse.createBySuccess("操作成功");
    }
}
